<?php
namespace app\bis\controller;
use think\Controller;
class Login extends Controller{
    //登陆页面
    public function index(){
         if(request()->isPost()){
           $data=input("post.");
          $username=$data["username"];
          $password=$data["password"];
          if(!$username){
           return   showmiss(0,"用户名不能为空");
          }
          if(!$password){
            return   showmiss(0,"密码不能为空");
          }
          
          $ret=model("BisAccount")->get(["username"=>$data["username"]]);
           if(!$ret || $ret->status !=1){
               return showmiss(0,"该用户不存在或未被过审");
           }
           //验证密码
           if($ret->password !=md5($data["password"].$ret->code)){
               return showmiss(0,"密码错误");
           }
           //保存信息并更新
           model("BisAccount")->updateById(["last_login_time"=>time()],$ret->id);
          //session保存登陆用户的信息bis 是作用域
            session("bisAccount",$ret,"bis");
            return showmiss(1,"登陆成功");
        }else{ 
            //获取session
            $account=session("bisAccount","","bis");
            if($account && $account->id){
                $this->redirect(url("index/index"));
            }
            
          return $this->fetch(); 
        }
    }
    
    //登陆退出
    public function logout(){
        //清除session 
           session(null,"bis");
        $this->redirect(url("login/index"));
    }
    
}
